package com.simple.javatraining.testing.Parser;

import com.simple.javatraining.testing.QuestionsFactory.QuestionFactory;
import com.simple.javatraining.testing.testinside.answers.ProposedAnswer;
import com.simple.javatraining.testing.testinside.questions.Question;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TextFileParser {
    private File file;

    public TextFileParser(File file) {
        this.file = file;
    }

    public List<Question> parseQuestions() {
        List<Question> listOfQuestions = new ArrayList<Question>();
        try {
            Scanner scanner = new Scanner(file);
            while (scanner.hasNextLine()) {
                String questionLine = scanner.nextLine();

                String questionType = parseType(questionLine);
                double delta = 0d;
                if (questionType.equals("QuestionWithAnswerWithinRange")) {
                    delta = parseDelta(questionLine);
                }
                String textOfQuestion = parseTextOfQuestion(questionLine);
                List<ProposedAnswer> variantsOfAnswer = parseAnswers(questionLine);

                QuestionFactory.createQuestion(questionType, textOfQuestion,
                                                                 variantsOfAnswer, delta);
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        List<Question> questions = new ArrayList<Question>(listOfQuestions);
        return questions;
    }

    private String parseTextOfQuestion(String string) {
        final String TEXT_OF_QUESTION =
                "(<text>)(.*?)(</text>)";
        final int STRING_GROUP_NUMBER = 2;

        return getFirstOccurrence(string, TEXT_OF_QUESTION, STRING_GROUP_NUMBER);
    }

    private String parseType(String string) {
        final String QUESTION_TYPE_PATTERN =
                "(<type=\"(.*?)\")";
        final int QUESTION_TYPE_GROUP_NUMBER = 2;

        return getFirstOccurrence(string, QUESTION_TYPE_PATTERN, QUESTION_TYPE_GROUP_NUMBER);
    }

    private List<ProposedAnswer> parseAnswers(String string) {
        final String ANSWER_PATTERN =
                "(<answer>)(.*?)(</answer>)";
        final int ANSWER_TEXT_GROUP_NUMBER = 2;

        return getAnswersOccurrences(string,
                ANSWER_PATTERN, ANSWER_TEXT_GROUP_NUMBER);
    }

    private double parseDelta(String string) {
        final String DEVIATION_PATTERN =
                "(<delta>(.*?)</delta>";
        final int DEVIATION_GROUP_NUMBER = 4;

        String delta = getFirstOccurrence(string,
                DEVIATION_PATTERN, DEVIATION_GROUP_NUMBER);
        return Double.parseDouble(delta);
    }

    private String getFirstOccurrence(String string,
                                      String stringPattern, int groupNumber) {
        Pattern pattern = Pattern.compile(stringPattern);
        Matcher matcher = pattern.matcher(string);

        if (!matcher.find())
            throw new IllegalArgumentException("Illegal string!");

        return matcher.group(groupNumber);
    }

    private List<ProposedAnswer> getAnswersOccurrences(String string,
                                          String stringPattern, int groupNumber) {
        Pattern pattern = Pattern.compile(stringPattern);
        Matcher matcher = pattern.matcher(string);

        List<ProposedAnswer> answers = new ArrayList<ProposedAnswer>();
        while (matcher.find()) {
            String textOfAnswer = matcher.group(groupNumber);
          //  boolean indicatorOfCorrect = matcher.group();
            //answers.add(new ProposedAnswer(textOfAnswer));
        }
       return answers;
    }
}
